<?php
/*
 * Created on 27 mai 2007
 *
 * http://www.art122-5.net
 * Marc Despland 
 * 
 * This file is used to test the MultiLanguageManager_controler
 * It needs phpunit to be installed
 * http://www.art122-5.net/index.php/PHP_Development_Tools
 * 
 * And you have to put it in the root directory of MediaWiki
 * 
 * Documentation about installation, how to use or modification could be found at :
 * http://www.art122-5.net/index.php/MultiLanguageManager_Extension
 *
 * Copyright (C) 2007  Marc Despland
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */
// Call MultiLanguageManager_Test::main() if this source file is executed directly.
if (!defined("PHPUnit_MAIN_METHOD")) {
    define("PHPUnit_MAIN_METHOD", "MultiLanguageManager_Test::main");
}

require_once "PHPUnit/Framework/TestCase.php";
require_once "PHPUnit/Framework/TestSuite.php";


include_once( dirname(__FILE__) .DIRECTORY_SEPARATOR . 'includes'.DIRECTORY_SEPARATOR .'WebStart.php' );
if (defined('MEDIAWIKI')) {
	# Initialize MediaWiki base class
	require_once( dirname(__FILE__).'/includes/Wiki.php' );
	$mediaWiki = new MediaWiki();
} else {
	define( 'MEDIAWIKI', true );

	# Load up some global defines.
	require_once( './includes/Defines.php' );
	# Include this site setttings
	require_once( './LocalSettings.php' );
	# Prepare MediaWiki
	require_once( 'includes/Setup.php' );

	# Initialize MediaWiki base class
	require_once( "includes/Wiki.php" );
	$mediaWiki = new MediaWiki();
}




//require_once 'extensions/MultiLanguageManager/MultiLanguageManager_controller.php';
require_once 'extensions/MultiLanguageManager/skins/Tests.php';
require( 'extensions/MultiLanguageManager/MultiLanguageManager.i18n.php' );


/**
 * Test class for MultiLanguageManager_controller.
 * Generated by PHPUnit_Util_Skeleton on 2007-05-30 at 20:45:26.
 */
class MultiLanguageManager_Test extends PHPUnit_Framework_TestCase {
	protected $user_name="Nosferatu";
	protected $configurationLoaded=FALSE;
	protected $notDefaultLanguage="fr";
	protected $invalidLanguage="aa";
	protected $dbw=NULL;
	protected $db_page_translation,$db_page, $db_page_language;
	protected $pageIdNotExists;
	protected $pageswithoutlanguage=array();
	
    /**
     * Runs the test methods of this class.
     *
     * @access public
     * @static
     */
    public static function main() {
        require_once "PHPUnit/TextUI/TestRunner.php";

        $suite  = new PHPUnit_Framework_TestSuite("MultiLanguageManager_Test");
        $result = PHPUnit_TextUI_TestRunner::run($suite);
    }

    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     *
     * @access protected
     */
    protected function setUp() {
    	global $mgAvailableLanguage;
    	global $wgContLanguageCode;
    	global $wgLanguageCode;
    	
    	$mgAvailableLanguage['en'] = 'multilanguagemanager_en';
		$mgAvailableLanguage['fr'] = 'multilanguagemanager_fr';
		$mgAvailableLanguage['de'] = 'multilanguagemanager_de';

		$wgContLanguageCode='en';
		$wgLanguageCode='en';
    	
    	//Define the parameter
    	$keys=array_keys($mgAvailableLanguage);
	    $i=0;
	    while (($i<count($keys)) && ($keys[$i]==$wgContLanguageCode)) $i++;
	    if ($i<count($keys)) {
	    	$this->notDefaultLanguage=$keys[$i];
	    }
	    while (array_key_exists($this->invalidLanguage,$mgAvailableLanguage)) $this->invalidLanguage.='a';
		$this->dbw = wfGetDB( DB_MASTER );
		$this->db_page=$this->dbw->tableName('page');
		$this->db_page_translation=$this->dbw->tableName('page_translation');
		$this->db_page_language  = $this->dbw->tableName('page_language');
		$i=1;
		$SQL="SELECT page_id FROM ! WHERE page_id = ?";
		$tbs=$this->dbw->safeQuery($SQL,$this->db_page,$i);
		while ($this->dbw->numRows($tbs)>0) {
			$i++;
			$tbs=$this->dbw->safeQuery($SQL,$this->db_page,$i);
		}
		$this->pageIdNotExists=$i;
		$SQL="SELECT p.page_id FROM (! p LEFT JOIN ! l ON p.page_id=l.page_id) LEFT JOIN ! t ON p.page_id=t.source WHERE l.page_id IS NULL and t.source IS NULL and p.page_namespace=0 ORDER BY p.page_id";
		$tbs=$this->dbw->safeQuery($SQL,$this->db_page,$this->db_page_language,$this->db_page_translation);
		$num=min(5,$this->dbw->numRows($tbs));
		for ($i=0;$i<$num;$i++) {
			$row = $this->dbw->fetchObject($tbs);
			$this->pageswithoutlanguage[$i]=$row->page_id;
		}
    }

    /**
     * Tears down the fixture, for example, close a network connection.
     * This method is called after a test is executed.
     *
     * @access protected
     */
    protected function tearDown() {
    	for ($i=0;$i<5;$i++) {
    		$SQL="DELETE FROM ! WHERE page_id=?";
			$tbs=$this->dbw->safeQuery($SQL,$this->db_page_language,$this->pageswithoutlanguage[$i]);
			$SQL="DELETE FROM ! WHERE translate=? or source=?";
			$tbs=$this->dbw->safeQuery($SQL,$this->db_page_translation,$this->pageswithoutlanguage[$i],$this->pageswithoutlanguage[$i]);
    	}
    }

    public function testGenereal() {
		$this->assertFalse(is_object(NULL));
		$a=2;
		$this->assertFalse(is_object($a));
		$a="essai";
		$this->assertFalse(is_object($a));
		$a=array("essai");
		$this->assertFalse(is_object($a));
		$a=Title::newFromID(1);
		$this->assertTrue(is_object($a));
		$this->assertNotEquals(get_class(NULL),"Title");
		$this->assertNotEquals(get_class(2),"Title");
		$this->assertNotEquals(get_class("essai"),"Title");
		$this->assertEquals(get_class($a),"Title");
		$this->assertFalse(is_numeric(NULL));
		$this->assertFalse(is_numeric("ese"));
		$a=Title::newFromID($this->pageIdNotExists);
		$this->assertFalse(is_object($a));
		//$this->assertEquals(get_class($a),"Title");
    }
/******************************************************************************
 * Controller Test
 */
    /**
     * TARGET : executeSetLanguageRequest
     * First we check invalid request
     */
    public function testExecuteSetLanguageRequest_InvalidRequest() {
    	global $wgContLanguageCode;
    	$controller=new MultiLanguageManager_controller();
    	$display=new MultiLanguageManager_displaytest();
    	$controller->setDisplay($display);
    	
    	//First we check invalid request
        $this->assertEquals($controller->executeSetLanguageRequest( NULL, NULL ),"multilanguagemanager_invalidrequest");
        $this->assertEquals($controller->executeSetLanguageRequest( $this->pageswithoutlanguage[0],$this->invalidLanguage  ),"multilanguagemanager_invalidrequest");
        $this->assertEquals($controller->executeSetLanguageRequest( "toto",$wgContLanguageCode),"multilanguagemanager_invalidrequest");
		$this->assertEquals($controller->executeSetLanguageRequest($this->pageIdNotExists,$wgContLanguageCode  ),"multilanguagemanager_invalidrequest");
    }	
	/**
	 * TARGET : executeSetLanguageRequest
	 * scenario1 :
	 * The source page is in the default language and has translations
	 * the translations will be removed from the database and the article will be put in the 
	 * new language.
	 * No translation data will existed for this page
	 * 
	 * $this->pageswithoutlanguage[0] will be the source page
	 * $this->pageswithoutlanguage[1] and $this->pageswithoutlanguage[2] will be declared as translation
	 */ 
    public function testExecuteSetLanguageRequest_Scenario1() {
    	global $wgContLanguageCode;
    	$controller=new MultiLanguageManager_controller();
    	$display=new MultiLanguageManager_displaytest();
    	$controller->setDisplay($display);

		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[1],$this->notDefaultLanguage);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[2],$this->notDefaultLanguage);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[1]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[2]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		
		//scenario1 : ready
		$this->assertEquals($controller->executeSetLanguageRequest($this->pageswithoutlanguage[0],$this->notDefaultLanguage  ),"multilanguagemanager_languageisset");

		//we control the data
		$tbs=$this->dbw->safeQuery("SELECT lang FROM ! WHERE page_id=?",$this->db_page_language,$this->pageswithoutlanguage[0]);
		$this->assertEquals($this->dbw->numRows($tbs),1);
		$row = $this->dbw->fetchObject($tbs);
		$this->assertEquals($row->lang,$this->notDefaultLanguage);
		//The page is in the right language
		$tbs=$this->dbw->safeQuery("SELECT source FROM ! WHERE source=? or translate=?",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[0]);
		$this->assertEquals($this->dbw->numRows($tbs),0);
		//No translation data available
    }

 	/**
 	 * TARGET : executeSetLanguageRequest
	 * scenario2 :
	 * The source page is not in the default language and it is the tranlation of 2 pages
	 * the translations will be removed from the database and the article will be put in the 
	 * new language.
	 * No translation data will existed for this page
	 * 
	 * $this->pageswithoutlanguage[0] will be the source page
	 * $this->pageswithoutlanguage[1] and $this->pageswithoutlanguage[2] will be declared as translated page
	 */ 
    public function testExecuteSetLanguageRequest_Scenario2() {
    	global $wgContLanguageCode;
    	$controller=new MultiLanguageManager_controller();
    	$display=new MultiLanguageManager_displaytest();
    	$controller->setDisplay($display);
		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[0],$this->notDefaultLanguage);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[1],$this->pageswithoutlanguage[0]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[2],$this->pageswithoutlanguage[0]);
		$this->assertEquals($this->dbw->affectedRows(),1);

		//scenario2 : ready
		$this->assertEquals($controller->executeSetLanguageRequest($this->pageswithoutlanguage[0],$wgContLanguageCode  ),"multilanguagemanager_languageisset");

		//we control the data
		$tbs=$this->dbw->safeQuery("SELECT lang FROM ! WHERE page_id=?",$this->db_page_language,$this->pageswithoutlanguage[0]);
		$this->assertEquals($this->dbw->numRows($tbs),0);
		//The page is in the right language
		$tbs=$this->dbw->safeQuery("SELECT source FROM ! WHERE source=? or translate=?",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[0]);
		$this->assertEquals($this->dbw->numRows($tbs),0);
		//No translation data available
    }

    /**
     * TARGET : executeDeleteTranslationRequest
     * First we check invalid request
     */
    public function testExecuteDeleteTranslationRequest_InvalidRequest() {
    	global $wgContLanguageCode;
    	$controller=new MultiLanguageManager_controller();
    	$display=new MultiLanguageManager_displaytest();
    	$controller->setDisplay($display);
    	
    	//First we check invalid request
    	$this->assertEquals($controller->executeDeleteTranslationRequest( ),"multilanguagemanager_invalidrequest");
        $this->assertEquals($controller->executeDeleteTranslationRequest( NULL, NULL ),"multilanguagemanager_invalidrequest");
        $this->assertEquals($controller->executeDeleteTranslationRequest( $this->pageswithoutlanguage[0],"toto"  ),"multilanguagemanager_invalidrequest");
        $this->assertEquals($controller->executeDeleteTranslationRequest( "toto",$this->pageswithoutlanguage[0]  ),"multilanguagemanager_invalidrequest");
    }	
	/**
	 * TARGET : executeDeleteTranslationRequest
	 * scenario1 :
	 * Page B is the tranlation of A and C
	 * Page D is the translation of A and C
	 * We remove the translation data for A et B
	 * 
	 * Page B still the translation of page C
	 * Page D still the translation of A and C
	 * 
	 * A : $this->pageswithoutlanguage[0] 
	 * B : $this->pageswithoutlanguage[1] 
	 * C : $this->pageswithoutlanguage[2] 
	 * D : $this->pageswithoutlanguage[3] 
	 */ 
    public function testExecuteDeleteTranslationRequest_Scenario1() {
    	global $wgContLanguageCode;
    	$controller=new MultiLanguageManager_controller();
    	$display=new MultiLanguageManager_displaytest();
    	$controller->setDisplay($display);
    	
    	$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[1],$this->notDefaultLanguage);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[3],$this->notDefaultLanguage);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[1]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[3]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[2],$this->pageswithoutlanguage[1]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[2],$this->pageswithoutlanguage[3]);
		$this->assertEquals($this->dbw->affectedRows(),1);

		//scenario1 : ready
		$this->assertEquals($controller->executeDeleteTranslationRequest($this->pageswithoutlanguage[0],$this->pageswithoutlanguage[1]  ),"multilanguagemanager_translationdeleted");
    	
    	//We check data
		$tbs=$this->dbw->safeQuery("SELECT * FROM ! WHERE page_id=? and lang=?",$this->db_page_language,$this->pageswithoutlanguage[1],$this->notDefaultLanguage);
		$this->assertEquals($this->dbw->numRows($tbs),1);
		$tbs=$this->dbw->safeQuery("SELECT * FROM ! WHERE page_id=? and lang=?",$this->db_page_language,$this->pageswithoutlanguage[3],$this->notDefaultLanguage);
		$this->assertEquals($this->dbw->numRows($tbs),1);
		$tbs=$this->dbw->safeQuery("SELECT * FROM ! WHERE source=? and translate=?",$this->db_page_translation,$this->pageswithoutlanguage[2],$this->pageswithoutlanguage[1]);
		$this->assertEquals($this->dbw->numRows($tbs),1);
		$tbs=$this->dbw->safeQuery("SELECT * FROM ! WHERE source=? and translate=?",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[3]);
		$this->assertEquals($this->dbw->numRows($tbs),1);
		$tbs=$this->dbw->safeQuery("SELECT * FROM ! WHERE source=? and translate=?",$this->db_page_translation,$this->pageswithoutlanguage[2],$this->pageswithoutlanguage[3]);
		$this->assertEquals($this->dbw->numRows($tbs),1);
    	
    }


    /**
     * TARGET : executeAddTranslationRequest
     * First we check invalid request
     */
    public function testExecuteAddTranslationRequest_InvalidRequest() {
    	global $wgContLanguageCode;
    	$controller=new MultiLanguageManager_controller();
    	$display=new MultiLanguageManager_displaytest();
    	$controller->setDisplay($display);
    	
    	//First we check invalid request
    	$this->assertEquals($controller->executeAddTranslationRequest(),"multilanguagemanager_invalidrequest");
        $this->assertEquals($controller->executeAddTranslationRequest( NULL, NULL ,NULL),"multilanguagemanager_invalidrequest");
        $this->assertEquals($controller->executeAddTranslationRequest( "toto", "tata" ,"titre"),"multilanguagemanager_invalidrequest");
        $this->assertEquals($controller->executeAddTranslationRequest( $this->pageswithoutlanguage[0], $this->pageswithoutlanguage[1] ,"titre"),"multilanguagemanager_invalidrequest");
        $this->assertEquals($controller->executeAddTranslationRequest( "", $this->pageIdNotExists ,"titre"),"multilanguagemanager_invalidrequest");
        $this->assertEquals($controller->executeAddTranslationRequest( "", $this->pageswithoutlanguage[0] ,3),"multilanguagemanager_invalidrequest");
        $this->assertEquals($controller->executeAddTranslationRequest( "", $this->pageswithoutlanguage[0] ,"zergzerghthyzghjvjv"),"multilanguagemanager_invalidtitle");
    }	

	/**
	 * TARGET : executeAddTranslationRequest
	 * scenario1 : (it will be used to test 6 cases)
	 * Page A,B is in the default language
	 * Page C,D is not in the default language
	 * 
	 * ("", A, titleB) failed
	 * ("", C, titleD) failed
	 * (A, "", titleB) failed
	 * (C, "", titleD) failed
	 * ("", C, titleB) success
	 * (A, ""; titleD) success
	 */ 
    public function testExecuteAddTranslationRequest_Scenario1() {
    	global $wgContLanguageCode;
    	$controller=new MultiLanguageManager_controller();
    	$display=new MultiLanguageManager_displaytest();
    	$controller->setDisplay($display);
    	
    	$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[2],$this->notDefaultLanguage);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[3],$this->notDefaultLanguage);
		$this->assertEquals($this->dbw->affectedRows(),1);
		
		$titleB=Title::newFromId($this->pageswithoutlanguage[1]);
		$this->assertEquals(get_class($titleB), "Title");
		$titleD=Title::newFromId($this->pageswithoutlanguage[3]);
		$this->assertEquals(get_class($titleD), "Title");	
		
		//scenario1 : ready
		$this->assertEquals($controller->executeAddTranslationRequest("", $this->pageswithoutlanguage[0], $titleB->getText()  ),"multilanguagemanager_translatedefault");
		$this->assertEquals($controller->executeAddTranslationRequest("", $this->pageswithoutlanguage[2], $titleD->getText()  ),"multilanguagemanager_sourcenotdefault");
		$this->assertEquals($controller->executeAddTranslationRequest($this->pageswithoutlanguage[0], "", $titleB->getText()  ),"multilanguagemanager_translatedefault");
		$this->assertEquals($controller->executeAddTranslationRequest($this->pageswithoutlanguage[2], "", $titleD->getText()  ),"multilanguagemanager_sourcenotdefault");
		$this->assertEquals($controller->executeAddTranslationRequest("", $this->pageswithoutlanguage[2], $titleB->getText()  ),"multilanguagemanager_translationadded");
		$this->assertEquals($controller->executeAddTranslationRequest($this->pageswithoutlanguage[0], "", $titleD->getText()  ),"multilanguagemanager_translationadded");
    	
	
    	//We check data
		$tbs=$this->dbw->safeQuery("SELECT * FROM ! WHERE source=? and translate=?",$this->db_page_translation,$this->pageswithoutlanguage[1],$this->pageswithoutlanguage[2]);
		$this->assertEquals($this->dbw->numRows($tbs),1);
		$tbs=$this->dbw->safeQuery("SELECT * FROM ! WHERE source=? and translate=?",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[3]);
		$this->assertEquals($this->dbw->numRows($tbs),1);    	
    }

    /**
     * TARGET : checkLanguage
     */
    public function testCheckLanguage() {
    	global $mgAvailableLanguage;
    	$controller=new MultiLanguageManager_controller();
        $this->assertFalse($controller->checkLanguage($this->invalidLanguage));
        $this->assertFalse($controller->checkLanguage());
        $this->assertFalse($controller->checkLanguage(NULL));
        $this->assertFalse($controller->checkLanguage(2));
        foreach( $mgAvailableLanguage as $key => $value) {
        	$this->assertTrue($controller->checkLanguage($key));
        }
    }

    /**
     * TARGET : getPageLanguage
     * Check Invalid request
     */
    public function testGetPageLanguage_invalidRequest() {
        global $wgContLanguageCode;
        $controller=new MultiLanguageManager_controller();
        $this->assertEquals($controller->getPageLanguage(),$wgContLanguageCode);
        $this->assertEquals($controller->getPageLanguage(NULL),$wgContLanguageCode);
        $this->assertEquals($controller->getPageLanguage($this->invalidLanguage),$wgContLanguageCode);
        $this->assertEquals($controller->getPageLanguage($this->pageIdNotExists),$wgContLanguageCode);
    }
    /**
     * TARGET : getPageLanguage
     * Scenario 1:  (it will be used to test 2 cases)
     * Page A in the default language
     * Page B not in the default language
     * 
     * We check the A language
     * We check the B language
     */
    public function testGetPageLanguage_scenario1() {
        global $wgContLanguageCode;
        $controller=new MultiLanguageManager_controller();
    	$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[1],$this->notDefaultLanguage);
		$this->assertEquals($this->dbw->affectedRows(),1);

		//scenario1 : ready
		$this->assertEquals($controller->getPageLanguage($this->pageswithoutlanguage[0]),$wgContLanguageCode);
		$this->assertEquals($controller->getPageLanguage($this->pageswithoutlanguage[1]),$this->notDefaultLanguage);
    }



     /**
     * TARGET : availableTranslation
     * Check Invalid request
     */
    public function testAvailableTranslation_invalidRequest() {
    	global $mgAvailableLanguage;
    	global $wgContLanguageCode;
		$controller=new MultiLanguageManager_controller();
		
		//check invalid parameters
		$this->assertEquals($controller->availableTranslation("",""),array());
		$this->assertEquals($controller->availableTranslation(Title::newFromID($this->pageswithoutlanguage[0]),$wgContLanguageCode),array());
		$this->assertEquals($controller->availableTranslation(Title::newFromID($this->pageswithoutlanguage[0]),$this->invalidLanguage),array());
    }


     /**
     * TARGET : availableTranslation
     * Scenario1 :
     * For this test, we know (@see setUp()) that :
     * 		-Default Language : 'en'
     * 		-Other Available language : 'fr', 'de'
     * 
     * Page A is in 'en'
     * Page B is in 'en'
     * Page C is in 'fr'
     * Page D is in 'de'
     * Page E is in 'de'
     * A is translated by C, D, E
     * B is translated by C
     * 
     */
    public function testAvailableTranslation_scenario1() {
    	global $mgAvailableLanguage;
    	global $wgContLanguageCode;
		$controller=new MultiLanguageManager_controller();
		
		//Datas initialization 
		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[2],'fr');
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[3],'de');
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[4],'de');
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[2]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[3]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[4]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[1],$this->pageswithoutlanguage[2]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		
		$this->assertEquals('en', $wgContLanguageCode);
		
		//Scenario 1 ready 
		$this->assertEquals($controller->availableTranslation(Title::newFromID($this->pageswithoutlanguage[0]),'fr'),array($this->pageswithoutlanguage[2]));
		$this->assertEquals($controller->availableTranslation(Title::newFromID($this->pageswithoutlanguage[0]),'de'),array($this->pageswithoutlanguage[3],$this->pageswithoutlanguage[4]));
		$this->assertEquals($controller->availableTranslation(Title::newFromID($this->pageswithoutlanguage[1]),'fr'),array($this->pageswithoutlanguage[2]));
		$this->assertEquals($controller->availableTranslation(Title::newFromID($this->pageswithoutlanguage[1]),'de'),array());
		$this->assertEquals($controller->availableTranslation(Title::newFromID($this->pageswithoutlanguage[3]),'en'),array($this->pageswithoutlanguage[0]));
		$this->assertEquals($controller->availableTranslation(Title::newFromID($this->pageswithoutlanguage[2]),'en'),array($this->pageswithoutlanguage[0],$this->pageswithoutlanguage[1]));
		$this->assertEquals($controller->availableTranslation(Title::newFromID($this->pageswithoutlanguage[2]),'de'),array($this->pageswithoutlanguage[3],$this->pageswithoutlanguage[4]));
    }    
    
     /**
     * TARGET : displaySelectLanguage
     * Check Invalid request
     */
    public function testDisplaySelectLanguage_invalidRequest() {
    	global $mgAvailableLanguage;
    	global $wgContLanguageCode;
    	$controller=new MultiLanguageManager_controller();
    	$display=new MultiLanguageManager_displaytest();
    	$controller->setDisplay($display);
    	$this->assertEquals($controller->displaySelectLanguage(),"");
    	$this->assertEquals($controller->displaySelectLanguage(NULL),"");
    	$this->assertEquals($controller->displaySelectLanguage(""),"");
    }
    	
    /**
     * TARGET : displaySelectLanguage
     * Scenario1 :
     * For this test, we know (@see setUp()) that :
     * 		-Default Language : 'en'
     * 		-Other Available language : 'fr', 'de'
     * 
     * Page A is in 'en'
     * Page B is in 'en'
     * Page C is in 'fr'
     * Page D is in 'de'
     * Page E is in 'de'
     * A is translated by C, D, E
     * B is translated by C
     * 
     */
     public function testDisplaySelectLanguage_scenario1() {
    	global $mgAvailableLanguage;
    	global $wgContLanguageCode;
    	global $allMessages;
    	$controller=new MultiLanguageManager_controller();
    	$display=new MultiLanguageManager_displaytest();
    	$controller->setDisplay($display);
		
		$this->assertEquals(array_key_exists('de',$allMessages),TRUE);
		
		//Datas initialization 
		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[2],'fr');
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[3],'de');
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[4],'de');
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[2]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[3]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[4]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[1],$this->pageswithoutlanguage[2]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		
		//Scenario1 ready
		$this->assertEquals($controller->displaySelectLanguage(Title::newFromId($this->pageswithoutlanguage[1])),"displayDirectPageLink(displayLanguageElement(fr),".$this->pageswithoutlanguage[2].")displayLanguageLinkPopup(displayLanguageElement(de),".wfMsgHtml('multilanguagemanager_notranslation',wfMsgHtml('multilanguagemanager_de')).")");
		$this->assertEquals($controller->displaySelectLanguage(Title::newFromId($this->pageswithoutlanguage[0])),"displayDirectPageLink(displayLanguageElement(fr),".$this->pageswithoutlanguage[2].")displayLanguageLinkPopup(displayLanguageElement(de),displayListTranslation(de,(".$this->pageswithoutlanguage[3]." ".$this->pageswithoutlanguage[4]." )))");
		
    }
    
    
     /**
     * TARGET : createDetailLanguagePage
     * Check Invalid request
     */ 
	public function testCreateDetailLanguagePage_InvalidRequest() {
    	$controller=new MultiLanguageManager_controller();
    	$display=new MultiLanguageManager_displaytest();
    	$controller->setDisplay($display);
    	$this->assertEquals($controller->createDetailLanguagePage(),"displayLanguagePolicy()");
    	$this->assertEquals($controller->createDetailLanguagePage(NULL),"displayLanguagePolicy()");
    	$this->assertEquals($controller->createDetailLanguagePage(""),"displayLanguagePolicy()");
    	$this->assertEquals($controller->createDetailLanguagePage("Ma Page inexistante"),"displayError(multilanguagemanager_notfound)");
    	$this->assertEquals($controller->createDetailLanguagePage("Ma Page inexistante/"),"displayError(multilanguagemanager_notfound)");
    	$this->assertEquals($controller->createDetailLanguagePage("Ma Page inexistante/aa"),"displayError(multilanguagemanager_notfound)");
	}
    	
     /**
     * TARGET : createDetailLanguagePage
     * Scenario1 :
     * For this test, we know (@see setUp()) that :
     * 		-Default Language : 'en'
     * 		-Other Available language : 'fr', 'de'
     * 		-We know that User 1 has the right to change language
     * 
     * Page A is in 'en'
     * Page B is in 'en'
     * Page C is in 'fr'
     * A is translated by C
     * B
     * 
     */
 	public function testCreateDetailLanguagePage_scenario1() {
 		global $wgUser;

    	$controller=new MultiLanguageManager_controller();
    	$display=new MultiLanguageManager_displaytest();
    	$controller->setDisplay($display);
 
		//Datas initialization 
		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[2],'fr');
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[2]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$pageA=Title::newFromID($this->pageswithoutlanguage[0]);
		$pageB=Title::newFromID($this->pageswithoutlanguage[1]);
		$pageC=Title::newFromID($this->pageswithoutlanguage[2]);
		
		//Scenario1 ready
		$this->assertEquals($controller->createDetailLanguagePage($pageA->getText()),"displayLanguagePolicy()displayChooseLanguage(en,".$this->pageswithoutlanguage[0].",FALSE,TRUE)displayItIsTranslatedBy(result,".$this->pageswithoutlanguage[0].",FALSE)");
		$this->assertEquals($controller->createDetailLanguagePage($pageB->getText()),"displayLanguagePolicy()displayChooseLanguage(en,".$this->pageswithoutlanguage[1].",FALSE,FALSE)displayItIsTranslatedBy(result,".$this->pageswithoutlanguage[1].",FALSE)");
		$this->assertEquals($controller->createDetailLanguagePage($pageC->getText()),"displayLanguagePolicy()displayChooseLanguage(fr,".$this->pageswithoutlanguage[2].",FALSE,TRUE)displayItTranslates(result,".$this->pageswithoutlanguage[2].",FALSE)");
		
		$oldUser=$wgUser;
		$wgUser=User::newFromName($this->user_name);
 
 		$this->assertEquals($controller->createDetailLanguagePage($pageA->getText()),"displayLanguagePolicy()displayChooseLanguage(en,".$this->pageswithoutlanguage[0].",TRUE,TRUE)displayItIsTranslatedBy(result,".$this->pageswithoutlanguage[0].",TRUE)");
		$this->assertEquals($controller->createDetailLanguagePage($pageB->getText()),"displayLanguagePolicy()displayChooseLanguage(en,".$this->pageswithoutlanguage[1].",TRUE,FALSE)displayItIsTranslatedBy(result,".$this->pageswithoutlanguage[1].",TRUE)");
		$this->assertEquals($controller->createDetailLanguagePage($pageC->getText()),"displayLanguagePolicy()displayChooseLanguage(fr,".$this->pageswithoutlanguage[2].",TRUE,TRUE)displayItTranslates(result,".$this->pageswithoutlanguage[2].",TRUE)");
		$wgUser=$oldUser;
 	} 
 /******************************************************************************
 * Global Test
 */
 	/**
 	 * Target wikiEncodedVersion()
 	 */
 	 
   	function testWikiEncodedVersion() {
   		global $wgVersion;
   		$saveVersion=$wgVersion;
   		$wgVersion="2.8.6";
   		$this->assertEquals(wikiEncodedVersion(),20806);
    	$wgVersion="3.12";
   		$this->assertEquals(wikiEncodedVersion(),31200);
   		$wgVersion="5";
   		$this->assertEquals(wikiEncodedVersion(),50000);
   		$wgVersion="toto.tata";
   		$this->assertEquals(wikiEncodedVersion(),0);
   		$wgVersion=$saveVersion;
    }
   	
   	function testMlChangeLanguageForGuest() {
   		if (wikiEncodedVersion()>=10800) {
   			
   		}
   		$this->assertTrue(TRUE);
   	}
   	
   	function testMultiLanguageManager() {
   		$manager=new MultiLanguageManager();
   		$this->assertTrue(is_subclass_of($manager,'SpecialPage'));
   	}
   	
   	function testEscapeFragmentForURL() {
   		$this->assertEquals(MultiLanguageManager::escapeFragmentForURL("le bo:pa.boéàè"),"le_bo:pa.bo.C3.A9.C3.A0.C3.A8");
   	}
   	
   	function testAddLanguageAction() {
   		global $wgTitle;
   		$ca=array();
   		$wgTitle=Title::newFromId($this->pageswithoutlanguage[0]);
   		MultiLanguageManager::addLanguageAction($ca);
   		$this->assertTrue(array_key_exists('language',$ca));
   	}
/******************************************************************************
 * So now we play the same test, after changing the user UI language to an other
 * supported language
 */
 
 
	/**
	 * TARGET : setUILanguage
	 */
 	public function testSetUILanguage() {
 		global $wgContLanguageCode,$wgLanguageCode;
 		global $wgUser;;
    	$controller=new MultiLanguageManager_controller();
 		$lang1=$controller->getPageLanguage($this->pageswithoutlanguage[0]);
 		$this->assertFalse($wgUser->isLoggedIn());
 		$this->assertEquals($lang1, $wgContLanguageCode);
 		$this->assertEquals($wgLanguageCode, $wgContLanguageCode);
 		MultiLanguageManager_controller::setUILanguage($this->notDefaultLanguage);
 		$this->assertNotEquals($wgLanguageCode, $wgContLanguageCode);
 		$lang2=$controller->getPageLanguage($this->pageswithoutlanguage[0]);
 		$this->assertEquals($lang2, $wgContLanguageCode);
 		$this->assertEquals($lang1, $lang2);

 	}
/******************************************************************************
 * Controller Test
 */
    /**
     * TARGET : executeSetLanguageRequest
     * First we check invalid request
     */
    public function testExecuteSetLanguageRequest_InvalidRequest_ndl() {
    	global $wgContLanguageCode;

    	$controller=new MultiLanguageManager_controller();
    	$display=new MultiLanguageManager_displaytest();
    	$controller->setDisplay($display);
    	MultiLanguageManager_controller::setUILanguage($this->notDefaultLanguage);
    	//First we check invalid request
        $this->assertEquals($controller->executeSetLanguageRequest( NULL, NULL ),"multilanguagemanager_invalidrequest");
        $this->assertEquals($controller->executeSetLanguageRequest( $this->pageswithoutlanguage[0],$this->invalidLanguage  ),"multilanguagemanager_invalidrequest");
        $this->assertEquals($controller->executeSetLanguageRequest( "toto",$wgContLanguageCode  ),"multilanguagemanager_invalidrequest");
		$this->assertEquals($controller->executeSetLanguageRequest($this->pageIdNotExists,$wgContLanguageCode  ),"multilanguagemanager_invalidrequest");
    }	
	/**
	 * TARGET : executeSetLanguageRequest
	 * scenario1 :
	 * The source page is in the default language and has translations
	 * the translations will be removed from the database and the article will be put in the 
	 * new language.
	 * No translation data will existed for this page
	 * 
	 * $this->pageswithoutlanguage[0] will be the source page
	 * $this->pageswithoutlanguage[1] and $this->pageswithoutlanguage[2] will be declared as translation
	 */ 
    public function testExecuteSetLanguageRequest_Scenario1_ndl() {
    	global $wgContLanguageCode;
    	$controller=new MultiLanguageManager_controller();
    	$display=new MultiLanguageManager_displaytest();
    	$controller->setDisplay($display);
		MultiLanguageManager_controller::setUILanguage($this->notDefaultLanguage);
		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[1],$this->notDefaultLanguage);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[2],$this->notDefaultLanguage);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[1]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[2]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		
		//scenario1 : ready
		$this->assertEquals($controller->executeSetLanguageRequest($this->pageswithoutlanguage[0],$this->notDefaultLanguage  ),"multilanguagemanager_languageisset");

		//we control the data
		$tbs=$this->dbw->safeQuery("SELECT lang FROM ! WHERE page_id=?",$this->db_page_language,$this->pageswithoutlanguage[0]);
		$this->assertEquals($this->dbw->numRows($tbs),1);
		$row = $this->dbw->fetchObject($tbs);
		$this->assertEquals($row->lang,$this->notDefaultLanguage);
		//The page is in the right language
		$tbs=$this->dbw->safeQuery("SELECT source FROM ! WHERE source=? or translate=?",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[0]);
		$this->assertEquals($this->dbw->numRows($tbs),0);
		//No translation data available
    }
  	/**
 	 * TARGET : executeSetLanguageRequest
	 * scenario2 :
	 * The source page is not in the default language and it is the tranlation of 2 pages
	 * the translations will be removed from the database and the article will be put in the 
	 * new language.
	 * No translation data will existed for this page
	 * 
	 * $this->pageswithoutlanguage[0] will be the source page
	 * $this->pageswithoutlanguage[1] and $this->pageswithoutlanguage[2] will be declared as translated page
	 */ 
    public function testExecuteSetLanguageRequest_Scenario2_ndl() {
    	global $wgContLanguageCode;
    	$controller=new MultiLanguageManager_controller();
    	$display=new MultiLanguageManager_displaytest();
    	$controller->setDisplay($display);
    	MultiLanguageManager_controller::setUILanguage($this->notDefaultLanguage);
		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[0],$this->notDefaultLanguage);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[1],$this->pageswithoutlanguage[0]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[2],$this->pageswithoutlanguage[0]);
		$this->assertEquals($this->dbw->affectedRows(),1);

		//scenario2 : ready
		$this->assertEquals($controller->executeSetLanguageRequest($this->pageswithoutlanguage[0],$wgContLanguageCode  ),"multilanguagemanager_languageisset");

		//we control the data
		$tbs=$this->dbw->safeQuery("SELECT lang FROM ! WHERE page_id=?",$this->db_page_language,$this->pageswithoutlanguage[0]);
		$this->assertEquals($this->dbw->numRows($tbs),0);
		//The page is in the right language
		$tbs=$this->dbw->safeQuery("SELECT source FROM ! WHERE source=? or translate=?",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[0]);
		$this->assertEquals($this->dbw->numRows($tbs),0);
		//No translation data available
    }

    /**
     * TARGET : executeDeleteTranslationRequest
     * First we check invalid request
     */
    public function testExecuteDeleteTranslationRequest_InvalidRequest_ndl() {
    	global $wgContLanguageCode;
    	$controller=new MultiLanguageManager_controller();
    	$display=new MultiLanguageManager_displaytest();
    	$controller->setDisplay($display);
    	MultiLanguageManager_controller::setUILanguage($this->notDefaultLanguage);
    	//First we check invalid request
    	$this->assertEquals($controller->executeDeleteTranslationRequest( ),"multilanguagemanager_invalidrequest");
        $this->assertEquals($controller->executeDeleteTranslationRequest( NULL, NULL ),"multilanguagemanager_invalidrequest");
        $this->assertEquals($controller->executeDeleteTranslationRequest( $this->pageswithoutlanguage[0],"toto"  ),"multilanguagemanager_invalidrequest");
        $this->assertEquals($controller->executeDeleteTranslationRequest( "toto",$this->pageswithoutlanguage[0]  ),"multilanguagemanager_invalidrequest");
    }	
	/**
	 * TARGET : executeDeleteTranslationRequest
	 * scenario1 :
	 * Page B is the tranlation of A and C
	 * Page D is the translation of A and C
	 * We remove the translation data for A et B
	 * 
	 * Page B still the translation of page C
	 * Page D still the translation of A and C
	 * 
	 * A : $this->pageswithoutlanguage[0] 
	 * B : $this->pageswithoutlanguage[1] 
	 * C : $this->pageswithoutlanguage[2] 
	 * D : $this->pageswithoutlanguage[3] 
	 */ 
    public function testExecuteDeleteTranslationRequest_Scenario1_ndl() {
    	global $wgContLanguageCode;
    	$controller=new MultiLanguageManager_controller();
    	$display=new MultiLanguageManager_displaytest();
    	$controller->setDisplay($display);
    	MultiLanguageManager_controller::setUILanguage($this->notDefaultLanguage);
    	$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[1],$this->notDefaultLanguage);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[3],$this->notDefaultLanguage);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[1]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[3]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[2],$this->pageswithoutlanguage[1]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[2],$this->pageswithoutlanguage[3]);
		$this->assertEquals($this->dbw->affectedRows(),1);

		//scenario1 : ready
		$this->assertEquals($controller->executeDeleteTranslationRequest($this->pageswithoutlanguage[0],$this->pageswithoutlanguage[1]  ),"multilanguagemanager_translationdeleted");
    	
    	//We check data
		$tbs=$this->dbw->safeQuery("SELECT * FROM ! WHERE page_id=? and lang=?",$this->db_page_language,$this->pageswithoutlanguage[1],$this->notDefaultLanguage);
		$this->assertEquals($this->dbw->numRows($tbs),1);
		$tbs=$this->dbw->safeQuery("SELECT * FROM ! WHERE page_id=? and lang=?",$this->db_page_language,$this->pageswithoutlanguage[3],$this->notDefaultLanguage);
		$this->assertEquals($this->dbw->numRows($tbs),1);
		$tbs=$this->dbw->safeQuery("SELECT * FROM ! WHERE source=? and translate=?",$this->db_page_translation,$this->pageswithoutlanguage[2],$this->pageswithoutlanguage[1]);
		$this->assertEquals($this->dbw->numRows($tbs),1);
		$tbs=$this->dbw->safeQuery("SELECT * FROM ! WHERE source=? and translate=?",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[3]);
		$this->assertEquals($this->dbw->numRows($tbs),1);
		$tbs=$this->dbw->safeQuery("SELECT * FROM ! WHERE source=? and translate=?",$this->db_page_translation,$this->pageswithoutlanguage[2],$this->pageswithoutlanguage[3]);
		$this->assertEquals($this->dbw->numRows($tbs),1);
    	
    }


    /**
     * TARGET : executeAddTranslationRequest
     * First we check invalid request
     */
    public function testExecuteAddTranslationRequest_InvalidRequest_ndl() {
    	global $wgContLanguageCode;
    	$controller=new MultiLanguageManager_controller();
    	$display=new MultiLanguageManager_displaytest();
    	$controller->setDisplay($display);
    	MultiLanguageManager_controller::setUILanguage($this->notDefaultLanguage);
    	//First we check invalid request
    	$this->assertEquals($controller->executeAddTranslationRequest(),"multilanguagemanager_invalidrequest");
        $this->assertEquals($controller->executeAddTranslationRequest( NULL, NULL ,NULL),"multilanguagemanager_invalidrequest");
        $this->assertEquals($controller->executeAddTranslationRequest( "toto", "tata" ,"titre"),"multilanguagemanager_invalidrequest");
        $this->assertEquals($controller->executeAddTranslationRequest( $this->pageswithoutlanguage[0], $this->pageswithoutlanguage[1] ,"titre"),"multilanguagemanager_invalidrequest");
        $this->assertEquals($controller->executeAddTranslationRequest( "", $this->pageIdNotExists ,"titre"),"multilanguagemanager_invalidrequest");
        $this->assertEquals($controller->executeAddTranslationRequest( "", $this->pageswithoutlanguage[0] ,3),"multilanguagemanager_invalidrequest");
        $this->assertEquals($controller->executeAddTranslationRequest( "", $this->pageswithoutlanguage[0] ,"zergzerghthyzghjvjv"),"multilanguagemanager_invalidtitle");
    }	

	/**
	 * TARGET : executeAddTranslationRequest
	 * scenario1 : (it will be used to test 6 cases)
	 * Page A,B is in the default language
	 * Page C,D is not in the default language
	 * 
	 * ("", A, titleB) failed
	 * ("", C, titleD) failed
	 * (A, "", titleB) failed
	 * (C, "", titleD) failed
	 * ("", C, titleB) success
	 * (A, ""; titleD) success
	 */ 
    public function testExecuteAddTranslationRequest_Scenario1_ndl() {
    	global $wgContLanguageCode;
    	$controller=new MultiLanguageManager_controller();
    	$display=new MultiLanguageManager_displaytest();
    	$controller->setDisplay($display);
    	MultiLanguageManager_controller::setUILanguage($this->notDefaultLanguage);
    	$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[2],$this->notDefaultLanguage);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[3],$this->notDefaultLanguage);
		$this->assertEquals($this->dbw->affectedRows(),1);
		
		$titleB=Title::newFromId($this->pageswithoutlanguage[1]);
		$this->assertEquals(get_class($titleB), "Title");
		$titleD=Title::newFromId($this->pageswithoutlanguage[3]);
		$this->assertEquals(get_class($titleD), "Title");	
		
		//scenario1 : ready
		$this->assertEquals($controller->executeAddTranslationRequest("", $this->pageswithoutlanguage[0], $titleB->getText()  ),"multilanguagemanager_translatedefault");
		$this->assertEquals($controller->executeAddTranslationRequest("", $this->pageswithoutlanguage[2], $titleD->getText()  ),"multilanguagemanager_sourcenotdefault");
		$this->assertEquals($controller->executeAddTranslationRequest($this->pageswithoutlanguage[0], "", $titleB->getText()  ),"multilanguagemanager_translatedefault");
		$this->assertEquals($controller->executeAddTranslationRequest($this->pageswithoutlanguage[2], "", $titleD->getText()  ),"multilanguagemanager_sourcenotdefault");
		$this->assertEquals($controller->executeAddTranslationRequest("", $this->pageswithoutlanguage[2], $titleB->getText()  ),"multilanguagemanager_translationadded");
		$this->assertEquals($controller->executeAddTranslationRequest($this->pageswithoutlanguage[0], "", $titleD->getText()  ),"multilanguagemanager_translationadded");
    	
	
    	//We check data
		$tbs=$this->dbw->safeQuery("SELECT * FROM ! WHERE source=? and translate=?",$this->db_page_translation,$this->pageswithoutlanguage[1],$this->pageswithoutlanguage[2]);
		$this->assertEquals($this->dbw->numRows($tbs),1);
		$tbs=$this->dbw->safeQuery("SELECT * FROM ! WHERE source=? and translate=?",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[3]);
		$this->assertEquals($this->dbw->numRows($tbs),1);    	
    }

    /**
     * TARGET : checkLanguage
     */
    public function testCheckLanguage_ndl() {
    	global $mgAvailableLanguage;
    	$controller=new MultiLanguageManager_controller();
    	MultiLanguageManager_controller::setUILanguage($this->notDefaultLanguage);
        $this->assertFalse($controller->checkLanguage($this->invalidLanguage));
        $this->assertFalse($controller->checkLanguage());
        $this->assertFalse($controller->checkLanguage(NULL));
        $this->assertFalse($controller->checkLanguage(2));
        foreach( $mgAvailableLanguage as $key => $value) {
        	$this->assertTrue($controller->checkLanguage($key));
        }
    }

    /**
     * TARGET : getPageLanguage
     * Check Invalid request
     */
    public function testGetPageLanguage_invalidRequest_ndl() {
        global $wgContLanguageCode;
        $controller=new MultiLanguageManager_controller();
        MultiLanguageManager_controller::setUILanguage($this->notDefaultLanguage);
        $this->assertEquals($controller->getPageLanguage(),$wgContLanguageCode);
        $this->assertEquals($controller->getPageLanguage(NULL),$wgContLanguageCode);
        $this->assertEquals($controller->getPageLanguage($this->invalidLanguage),$wgContLanguageCode);
        $this->assertEquals($controller->getPageLanguage($this->pageIdNotExists),$wgContLanguageCode);
    }
    /**
     * TARGET : getPageLanguage
     * Scenario 1:  (it will be used to test 2 cases)
     * Page A in the default language
     * Page B not in the default language
     * 
     * We check the A language
     * We check the B language
     */
    public function testGetPageLanguage_scenario1_ndl() {
        global $wgContLanguageCode;
        $controller=new MultiLanguageManager_controller();
        MultiLanguageManager_controller::setUILanguage($this->notDefaultLanguage);
    	$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[1],$this->notDefaultLanguage);
		$this->assertEquals($this->dbw->affectedRows(),1);

		//scenario1 : ready
		$this->assertEquals($controller->getPageLanguage($this->pageswithoutlanguage[0]),$wgContLanguageCode);
		$this->assertEquals($controller->getPageLanguage($this->pageswithoutlanguage[1]),$this->notDefaultLanguage);
    }



     /**
     * TARGET : availableTranslation
     * Check Invalid request
     */
    public function testAvailableTranslation_invalidRequest_ndl() {
    	global $mgAvailableLanguage;
    	global $wgContLanguageCode;
		$controller=new MultiLanguageManager_controller();
		MultiLanguageManager_controller::setUILanguage($this->notDefaultLanguage);
		//check invalid parameters
		$this->assertEquals($controller->availableTranslation("",""),array());
		$this->assertEquals($controller->availableTranslation(Title::newFromID($this->pageswithoutlanguage[0]),$wgContLanguageCode),array());
		$this->assertEquals($controller->availableTranslation(Title::newFromID($this->pageswithoutlanguage[0]),$this->invalidLanguage),array());
    }


     /**
     * TARGET : availableTranslation
     * Scenario1 :
     * For this test, we know (@see setUp()) that :
     * 		-Default Language : 'en'
     * 		-Other Available language : 'fr', 'de'
     * 
     * Page A is in 'en'
     * Page B is in 'en'
     * Page C is in 'fr'
     * Page D is in 'de'
     * Page E is in 'de'
     * A is translated by C, D, E
     * B is translated by C
     * 
     */
    public function testAvailableTranslation_scenario1_ndl() {
    	global $mgAvailableLanguage;
    	global $wgContLanguageCode;
		$controller=new MultiLanguageManager_controller();
		MultiLanguageManager_controller::setUILanguage($this->notDefaultLanguage);
		//Datas initialization 
		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[2],'fr');
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[3],'de');
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[4],'de');
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[2]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[3]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[4]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[1],$this->pageswithoutlanguage[2]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		
		$this->assertEquals('en', $wgContLanguageCode);
		
		//Scenario 1 ready 
		$this->assertEquals($controller->availableTranslation(Title::newFromID($this->pageswithoutlanguage[0]),'fr'),array($this->pageswithoutlanguage[2]));
		$this->assertEquals($controller->availableTranslation(Title::newFromID($this->pageswithoutlanguage[0]),'de'),array($this->pageswithoutlanguage[3],$this->pageswithoutlanguage[4]));
		$this->assertEquals($controller->availableTranslation(Title::newFromID($this->pageswithoutlanguage[1]),'fr'),array($this->pageswithoutlanguage[2]));
		$this->assertEquals($controller->availableTranslation(Title::newFromID($this->pageswithoutlanguage[1]),'de'),array());
		$this->assertEquals($controller->availableTranslation(Title::newFromID($this->pageswithoutlanguage[3]),'en'),array($this->pageswithoutlanguage[0]));
		$this->assertEquals($controller->availableTranslation(Title::newFromID($this->pageswithoutlanguage[2]),'en'),array($this->pageswithoutlanguage[0],$this->pageswithoutlanguage[1]));
		$this->assertEquals($controller->availableTranslation(Title::newFromID($this->pageswithoutlanguage[2]),'de'),array($this->pageswithoutlanguage[3],$this->pageswithoutlanguage[4]));
    }    
    
     /**
     * TARGET : displaySelectLanguage
     * Check Invalid request
     */
    public function testDisplaySelectLanguage_invalidRequest_ndl() {
    	global $mgAvailableLanguage;
    	global $wgContLanguageCode;
    	$controller=new MultiLanguageManager_controller();
    	MultiLanguageManager_controller::setUILanguage($this->notDefaultLanguage);
    	$display=new MultiLanguageManager_displaytest();
    	$controller->setDisplay($display);
    	$this->assertEquals($controller->displaySelectLanguage(),"");
    	$this->assertEquals($controller->displaySelectLanguage(NULL),"");
    	$this->assertEquals($controller->displaySelectLanguage(""),"");
    }
    	
    /**
     * TARGET : displaySelectLanguage
     * Scenario1 :
     * For this test, we know (@see setUp()) that :
     * 		-Default Language : 'en'
     * 		-Other Available language : 'fr', 'de'
     * 
     * Page A is in 'en'
     * Page B is in 'en'
     * Page C is in 'fr'
     * Page D is in 'de'
     * Page E is in 'de'
     * A is translated by C, D, E
     * B is translated by C
     * 
     */
     public function testDisplaySelectLanguage_scenario1_ndl() {
    	global $mgAvailableLanguage;
    	global $wgContLanguageCode;
    	global $allMessages;
    	$controller=new MultiLanguageManager_controller();
    	$display=new MultiLanguageManager_displaytest();
    	$controller->setDisplay($display);
		MultiLanguageManager_controller::setUILanguage($this->notDefaultLanguage);
		$this->assertEquals(array_key_exists('de',$allMessages),TRUE);
		
		//Datas initialization 
		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[2],'fr');
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[3],'de');
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[4],'de');
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[2]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[3]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[4]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[1],$this->pageswithoutlanguage[2]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		
		//Scenario1 ready
		$this->assertEquals($controller->displaySelectLanguage(Title::newFromId($this->pageswithoutlanguage[1])),"displayDirectPageLink(displayLanguageElement(fr),".$this->pageswithoutlanguage[2].")displayLanguageLinkPopup(displayLanguageElement(de),".wfMsgHtml('multilanguagemanager_notranslation',wfMsgHtml('multilanguagemanager_de')).")");
		$this->assertEquals($controller->displaySelectLanguage(Title::newFromId($this->pageswithoutlanguage[0])),"displayDirectPageLink(displayLanguageElement(fr),".$this->pageswithoutlanguage[2].")displayLanguageLinkPopup(displayLanguageElement(de),displayListTranslation(de,(".$this->pageswithoutlanguage[3]." ".$this->pageswithoutlanguage[4]." )))");
		
    }
    
    
     /**
     * TARGET : createDetailLanguagePage
     * Check Invalid request
     */ 
	public function testCreateDetailLanguagePage_InvalidRequest_ndl() {
    	$controller=new MultiLanguageManager_controller();
    	$display=new MultiLanguageManager_displaytest();
    	$controller->setDisplay($display);
    	MultiLanguageManager_controller::setUILanguage($this->notDefaultLanguage);
    	$this->assertEquals($controller->createDetailLanguagePage(),"displayLanguagePolicy()");
    	$this->assertEquals($controller->createDetailLanguagePage(NULL),"displayLanguagePolicy()");
    	$this->assertEquals($controller->createDetailLanguagePage(""),"displayLanguagePolicy()");
    	$this->assertEquals($controller->createDetailLanguagePage("Ma Page inexistante"),"displayError(multilanguagemanager_notfound)");
    	$this->assertEquals($controller->createDetailLanguagePage("Ma Page inexistante/"),"displayError(multilanguagemanager_notfound)");
    	$this->assertEquals($controller->createDetailLanguagePage("Ma Page inexistante/aa"),"displayError(multilanguagemanager_notfound)");
	}
    	
     /**
     * TARGET : createDetailLanguagePage
     * Scenario1 :
     * For this test, we know (@see setUp()) that :
     * 		-Default Language : 'en'
     * 		-Other Available language : 'fr', 'de'
     * 		-We know that User 1 has the right to change language
     * 
     * Page A is in 'en'
     * Page B is in 'en'
     * Page C is in 'fr'
     * A is translated by C
     * B
     * 
     */
 	public function testCreateDetailLanguagePage_scenario1_ndl() {
 		global $wgUser;

    	$controller=new MultiLanguageManager_controller();
    	$display=new MultiLanguageManager_displaytest();
    	$controller->setDisplay($display);
 		MultiLanguageManager_controller::setUILanguage($this->notDefaultLanguage);
 		
		//Datas initialization 
		$this->dbw->safeQuery("INSERT INTO ! (page_id, lang) VALUES (?, ?)",$this->db_page_language,$this->pageswithoutlanguage[2],'fr');
		$this->assertEquals($this->dbw->affectedRows(),1);
		$this->dbw->safeQuery("INSERT INTO ! (source, translate) VALUES (?, ?)",$this->db_page_translation,$this->pageswithoutlanguage[0],$this->pageswithoutlanguage[2]);
		$this->assertEquals($this->dbw->affectedRows(),1);
		$pageA=Title::newFromID($this->pageswithoutlanguage[0]);
		$pageB=Title::newFromID($this->pageswithoutlanguage[1]);
		$pageC=Title::newFromID($this->pageswithoutlanguage[2]);
		
		//Scenario1 ready
		$this->assertEquals($controller->createDetailLanguagePage($pageA->getText()),"displayLanguagePolicy()displayChooseLanguage(en,".$this->pageswithoutlanguage[0].",FALSE,TRUE)displayItIsTranslatedBy(result,".$this->pageswithoutlanguage[0].",FALSE)");
		$this->assertEquals($controller->createDetailLanguagePage($pageB->getText()),"displayLanguagePolicy()displayChooseLanguage(en,".$this->pageswithoutlanguage[1].",FALSE,FALSE)displayItIsTranslatedBy(result,".$this->pageswithoutlanguage[1].",FALSE)");
		$this->assertEquals($controller->createDetailLanguagePage($pageC->getText()),"displayLanguagePolicy()displayChooseLanguage(fr,".$this->pageswithoutlanguage[2].",FALSE,TRUE)displayItTranslates(result,".$this->pageswithoutlanguage[2].",FALSE)");
		$oldUser=$wgUser;
		$wgUser=User::newFromName($this->user_name);
 
 		$this->assertEquals($controller->createDetailLanguagePage($pageA->getText()),"displayLanguagePolicy()displayChooseLanguage(en,".$this->pageswithoutlanguage[0].",TRUE,TRUE)displayItIsTranslatedBy(result,".$this->pageswithoutlanguage[0].",TRUE)");
		$this->assertEquals($controller->createDetailLanguagePage($pageB->getText()),"displayLanguagePolicy()displayChooseLanguage(en,".$this->pageswithoutlanguage[1].",TRUE,FALSE)displayItIsTranslatedBy(result,".$this->pageswithoutlanguage[1].",TRUE)");
		$this->assertEquals($controller->createDetailLanguagePage($pageC->getText()),"displayLanguagePolicy()displayChooseLanguage(fr,".$this->pageswithoutlanguage[2].",TRUE,TRUE)displayItTranslates(result,".$this->pageswithoutlanguage[2].",TRUE)");
		$wgUser=$oldUser;
 	} 

 	
}

// Call MultiLanguageManager_Test::main() if this source file is executed directly.
if (PHPUnit_MAIN_METHOD == "MultiLanguageManager_Test::main") {
    MultiLanguageManager_Test::main();
}
?>
